package vocation;

import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import weaver.general.GCONST;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class beforlog {


    @Before
    public void init(){
        GCONST.setRootPath("");
        GCONST.setServerName("ecology");

         System.out.println("----------------开始----------------");
    }

    @After
    public void demoAfter() throws  Exception{

       System.out.println("-----------------结束---------------");


    }


    public HttpServletRequest CreateHttpServletOrmap(Map<String,Object> map){
        HttpServletRequest httpServletRequest = (HttpServletRequest) EasyMock.createMock(HttpServletRequest.class);
     //   Map<String, String> map2 = new HashMap<>();
        for (String key:map.keySet() ) {
            //    String[] values=
          //  map2.put(key,map.get(key).toString());
            EasyMock.expect(httpServletRequest.getParameter(key)).andReturn(map.get(key).toString());

        }
        //  EasyMock.expect(httpServletRequest.getParameterMap()).andReturn(map2);
        EasyMock.expect(httpServletRequest.getParameter("2123")).andReturn("123");
        HttpSession session = EasyMock.createStrictMock(HttpSession.class);
        //session管理

        EasyMock.expect(httpServletRequest.getParameter("f_weaver_belongto_userid")).andReturn("1");
        EasyMock.expect(httpServletRequest.getParameter("f_weaver_belongto_usertype")).andReturn("0");
        EasyMock.expect(session.getAttribute("weaver_user@bean")).andReturn(new User(290464));
        EasyMock.expect(httpServletRequest.getSession(true)).andStubReturn(session);
        EasyMock.replay(httpServletRequest, session);
        return httpServletRequest;
    }



    public HttpServletRequest CreateHttpServletOr(Map<String,Object> map){
        HttpServletRequest httpServletRequest = (HttpServletRequest) EasyMock.createMock(HttpServletRequest.class);
        Map<String, String[]> map2 = new HashMap<>();
        for (String key:map.keySet() ) {
            //    String[] values=
            map2.put(key,map.get(key).toString().split(""));
            EasyMock.expect(httpServletRequest.getParameter(key)).andReturn(map.get(key).toString());

        }
        //  EasyMock.expect(httpServletRequest.getParameterMap()).andReturn(map2);
        EasyMock.expect(httpServletRequest.getParameter("2123")).andReturn("123");
        HttpSession session = EasyMock.createStrictMock(HttpSession.class);
        //session管理

        EasyMock.expect(httpServletRequest.getParameter("f_weaver_belongto_userid")).andReturn("26347");
        EasyMock.expect(httpServletRequest.getParameter("f_weaver_belongto_usertype")).andReturn("0");
        EasyMock.expect(session.getAttribute("weaver_user@bean")).andReturn(new User(26347));
        EasyMock.expect(httpServletRequest.getSession(true)).andStubReturn(session);
        EasyMock.replay(httpServletRequest, session);
        return httpServletRequest;
    }



    public HttpServletRequest CreateHttpServletworkflowtodu(Map<String,Object> map,Map<String,String> headmap){
        HttpServletRequest httpServletRequest = (HttpServletRequest) EasyMock.createMock(HttpServletRequest.class);
        Map<String, String> map2 = new HashMap<>();
        for (String key:map.keySet() ) {
        //    String[] values=

            EasyMock.expect(httpServletRequest.getParameter(key)).andReturn(map.get(key).toString());

        }
        for (String key:headmap.keySet() ) {
            //    String[] values=

            EasyMock.expect(httpServletRequest.getHeader(key)).andReturn(headmap.get(key).toString());

        }
        EasyMock.expect(httpServletRequest.getParameter("2123")).andReturn("123");
        HttpSession session = EasyMock.createStrictMock(HttpSession.class);
        //session管理
        EasyMock.expect(httpServletRequest.getParameter("f_weaver_belongto_userid")).andReturn("26347");
        EasyMock.expect(httpServletRequest.getParameter("f_weaver_belongto_usertype")).andReturn("0");
        EasyMock.expect(session.getAttribute("weaver_user@bean")).andReturn(new User(26347));
        EasyMock.expect(httpServletRequest.getSession(true)).andStubReturn(session);
        EasyMock.replay(httpServletRequest, session);
        return httpServletRequest;
    }

    public  String getUrl(){
       String value= this.getClass().getResource("/").getPath();
       if(value.length()>1){
           return value.substring(1,value.length()-1);
       }
       return  value;
    }
    public String loadjson(String path) {
        path=getUrl()+path;
        String str = "";
        try {

            File file = new File(path);// 定义一个file对象，用来初始化FileReader
            FileReader reader = new FileReader(file);// 定义一个fileReader对象，用来初始化BufferedReader
            BufferedReader bReader = new BufferedReader(reader);// new一个BufferedReader对象，将文件内容读取到缓存
            StringBuilder sb = new StringBuilder();// 定义一个字符串缓存，将字符串存放缓存中
            String s = "";
            while ((s = bReader.readLine()) != null) {// 逐行读取文件内容，不读取换行符和末尾的空格
                sb.append(s + "\n");// 将读取的字符串添加换行符后累加存放在缓存中
                System.out.println(s);
            }
            bReader.close();
            str = sb.toString();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}

